layui.config({
  base: '/' //假设这是你存放拓展模块的根目录
}).extend({ //设定模块别名
  murl: 'url' //如果 mymod.js 是在根目录，也可以不用设定别名
});
layui.use(['form','murl','layer','element','laydate','laytpl'], function () {
     var laydate = layui.laydate,laytpl = layui.laytpl ,form = layui.form, murl=layui.murl,id = murl.get("id");
     var layerTips = parent.layer === undefined ? layui.layer : parent.layer;
      $("#id").val(id);
      $.ajax({
           url: '/admin/resources/getInfo?id='+id,
           type: 'get',
           dataType: "json",
           success: function (data) {
               if(data.rel){
                 setLabelValues(data.result);
                 $("#addr").html(chechUndefined(data.result.provice)+" "+chechUndefined(data.result.city)+" "+chechUndefined(data.result.county)+" "+chechUndefined(data.result.town)+" "+chechUndefined(data.result.address));
                 var fileImgs=data.result.files.split(";");
                 var imgHtml="";
                 for(var i=0;i<fileImgs.length;i++ ){
                  imgHtml+=" <img src='"+fileImgs[i]+"' width='200'  class='img-rounded' />"
                 }
                 $("#filesImg").html(imgHtml);
                   var viewer = new Viewer(document.getElementById('filesImg'));
               }else{
                 layerTips.msg(data.msg);
               }
           }
       });
    form.on('submit(save)', function(data){
        data.field.status="1";
        submitForm(data);
       return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
     });

     form.on('submit(cancel)', function(data){
       data.field.status="2";
        if($("#replay2").val()==""){
           layerTips.msg('请填写回执');
           return false;
         }
        submitForm(data);
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
      });
});

function submitForm(data){
  $.ajax({
     url: '/admin/resources/save',
     type: 'post',
     data: data.field,
     dataType: "json",
     success: function (data) {
         var layerTips = parent.layer === undefined ? layui.layer : parent.layer;
         if(data.rel){
            layerTips.msg('保存成功');
             window.setTimeout(function () {
                         parent.tab.deleteTab(parent.tab.getCurrentTabId(),true);
                     },1000);
         }else{
           layerTips.msg(data.msg);
         }
     }
   });
}

function setLabelValues(data) {
    for (var p in data) {
        $("#" + p + "").html(data[p]);
    }
}

function setInputValues(data) {
    for (var p in data) {
        $("#" + p + "").val(data[p]);
    }
}

function chechUndefined(val) {
    if(val){
        return val;
    }else{
        return '';
    }
}
